import { Component, OnInit } from '@angular/core';
import { YYInfo } from 'src/model/yy/YYInfo';
import { YY1API } from 'src/api/YY1API';
import { ActivatedRoute, Params } from '@angular/router';
import { IDReq } from 'src/model/comm/IDReq';
import { YYModifyRightReq } from 'src/model/yy/YYModifyRightReq';

@Component({
  selector: 'app-yy1-detail',
  templateUrl: './yy1-detail.page.html',
  styleUrls: ['./yy1-detail.page.scss'],
})
export class Yy1DetailPage implements OnInit {

  // id
  id: number;
  // YY1内容
  item: YYInfo = new YYInfo();

  constructor(
    private yy1API: YY1API,
    private activeRoute: ActivatedRoute,
  ) { }

  ngOnInit() {
    this.activeRoute.queryParams.subscribe((params: Params) => {
      this.id = params.id;

      // 初始化
      this.initPage();
    });
  }

  // 查询
  async search(showLoader: boolean = true): Promise<YYInfo> {
    // 条件
    let req = new IDReq();
    req.id = this.id;

    // 查询列表
    const lstInfo = (await this.yy1API.getByID(req, showLoader)).info;
    return lstInfo;
  }

  // 初始化
  async initPage() {
    // 查询
    this.item = await this.search();
  }

  // 点击踩
  async downClick() {
    let req = new YYModifyRightReq();
    req.id = this.id;
    req.offset = -1;

    const isOK = (await this.yy1API.modifyRight(req)).info;
    if (this.item != null) {
      this.item.right -= 1;
    }
  }

  // 点击赞
  async upClick() {
    let req = new YYModifyRightReq();
    req.id = this.id;
    req.offset = 1;

    const isOK = (await this.yy1API.modifyRight(req)).info;
    if (this.item != null) {
      this.item.right += 1;
    }
  }
}
